<?php

namespace App\Models;

use App\Util\Tool;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;

//医护工作室分类
class WorkRoomSortCategory extends Model
{

    use ModelTree, AdminBuilder;

    protected $table = 'c_work_room_sort_categories';

    protected $fillable = ['pid', 'classify_name', 'sort'];


    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setOrderColumn('sort'); // 排序
        $this->setTitleColumn('classify_name'); // 标题
        $this->setParentColumn('pid');//删除使用的pid
    }

    // 该分类下的品牌
    public function brand()
    {
        return $this->hasMany(BrandModel::class, 'cate_id', $this->getKeyName());
    }

    public function getOptions()
    {
        $options = $this->select('id', 'classify_name as text')->get();
        $selectOption = [];
        foreach ($options as $option) {
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }

    /**
     * 该分类的子分类
     */
    public function child()
    {
        return $this->hasMany(get_class($this), 'pid', $this->getKeyName());
    }

    /**
     * 该分类的父分类
     */
    public function parent()
    {
        return $this->hasOne(get_class($this), $this->getKeyName(), 'pid');
    }

    /**获取分类名称
     * @param $id
     * @return mixed
     */
    public function getName($id)
    {
        $res = $this->select('classify_name')->find($id);
        if (!empty($res)) {
            return $res->classify_name;
        } else {
            return '';
        }
    }

    /***
     * @return array
     * 工作室自己的分类
     */
    public function getWorkSelfCategoryList($param)
    {
        $where = [];
        $limit = 15;
        if (isset($param['pid'])) {
            $where[] = ['pid', '=', $param['pid']];
        }
        if (isset($param['state'])) {
            $where[] = ['state', '=', $param['state']];
        }
//        if (!empty($param['limit'])) {
//            $limit = $param['limit'];
//        }
        if (!empty($param['work_id'])) {
            $where[] = ['work_id', '=', $param['work_id']];
        }
        $res = $this::orderByDesc("sort")->where($where)->get();
        if(!empty($res)){
            $tree = self::array_group_by($res->toArray());
            ksort($tree);
            return $tree;
        }
        return [
            1 => [],
            2 => [],
            3 => [],
            4 => [],
        ];

    }

    public static function array_group_by($list)
    {
        if(empty($list)){
            return [
                1 => [],
                2 => [],
                3 => [],
                4 => [],
            ];
        }
        $newArray = array();
        foreach ($list as $v) {
            $newArray[$v['pid']][] = $v;
        }
        return  $newArray;
    }

    /***
     * @param $filter
     * 添加工作室自己的分类
     */
    public function addSelfCategory($filter)
    {
        if (isset($filter['id'])) {
            $info = $this->where([["id", '=', $filter['id']]])->first();
            $info->pid = isset($filter['pid']) ? $filter['pid'] : $info->pid;
            $info->state = isset($filter['state']) ? $filter['state'] : $info->state;
            $info->work_id = isset($filter['work_id']) ? $filter['work_id'] : $info->work_id;
            $info->classify_name = isset($filter['classify_name']) ? $filter['classify_name'] : $info->classify_name;
            $info->sort = isset($filter['sort']) ? $filter['sort'] : $info->sort;
            return $info->save();
        } else {
            $this->pid = isset($filter['pid']) && $filter['pid'] >0 ? $filter['pid'] : 0;
            $this->state = isset($filter['state']) ? $filter['state'] : 0;
            $this->work_id = $filter['work_id'];
            $this->classify_name = $filter['classify_name'];
            $this->sort = isset($filter['sort'])?$filter['sort']:99;
            return $this->save();
        }
    }

    /**
     * @param $id
     * @return bool|string
     * 删除分类
     */
    public static function del($id)
    {
        $result = self::where([['id', '=', $id]])->first();
        if ($result) {
            $first = (new WorkRoomSortCategory())->where([['work_id','=',$result->work_id],['pid','=',$result->pid]])->first();
            if ($result->id == $first->id) {
                return "系统默认分类不能删除!";
            }
            self::where([['id', '=', $id]])->delete();
            return true;
        }
        return "暂无数据";
    }

}
